package com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.BannerBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.util.GlideImageLoader;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.Utils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.HomeVipActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.shop.act.GoodDetailActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/5/9.
 * Desc   :
 */

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 1;
    private static final int TYPE_ITEM = 2;

    private Context mContext;
    List<ShopListBean> goodList = new ArrayList<>();
    List<BannerBean> bannerList = new ArrayList<>();
    private ArrayList<String> bannerLinks;

    public ShopAdapter(Context mContext, List<ShopListBean> goodList, List<BannerBean> bannerList) {
        this.mContext = mContext;
        this.goodList = goodList;
        this.bannerList = bannerList;
    }

    public void setData(List<ShopListBean> goodList) {
        this.goodList = goodList;
        notifyDataSetChanged();
    }

    public void addData(List<ShopListBean> goodList) {
        this.goodList.addAll(goodList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_ITEM;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.header_shop, parent, false);
            return new ShopHeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_shop, parent, false);
            return new GoodItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof GoodItemViewHolder) {
            ShopListBean bean = goodList.get(position - 1);
            holder.itemView.setOnClickListener(view -> mContext.startActivity(GoodDetailActivity.createIntent(mContext, bean.getId(), null)));
            GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + (bean.getPic().split(",")[0]), R.mipmap.img_error, R.mipmap.img_error, ((GoodItemViewHolder) holder).ivGoodPic);
            if (bean.getSelf_support()!=null){
                ((GoodItemViewHolder) holder).ivSelfSupport.setVisibility(bean.getSelf_support().equals("y") ? View.VISIBLE : View.GONE);
            }
            ((GoodItemViewHolder) holder).tvGoodName.setText(bean.getName());
            ((GoodItemViewHolder) holder).tvVipPrice.setText(String.format(ResourcesUtils.getString(R.string.vip_good_price), bean.getPrice_vip()));
            ((GoodItemViewHolder) holder).tvNormalPrice.setText(String.format(ResourcesUtils.getString(R.string.normal_good_price), bean.getPrice()));
            ((GoodItemViewHolder) holder).ivBecomVip.setOnClickListener(v -> {
                mContext.startActivity(HomeVipActivity.createIntent(mContext));
            });
        } else if (holder instanceof ShopHeaderViewHolder) {
            //动态设置轮播图高度(2:1)
            ViewGroup.LayoutParams layoutParams = ((ShopHeaderViewHolder) holder).shopBanner.getLayoutParams();
            layoutParams.height = (int) (Utils.getScreenWidth(mContext) * 0.304);
            ((ShopHeaderViewHolder) holder).shopBanner.requestLayout();
            //设置banner样式
            ((ShopHeaderViewHolder) holder).shopBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            //设置图片加载器
            ((ShopHeaderViewHolder) holder).shopBanner.setImageLoader(new GlideImageLoader());
            //设置banner动画效果
            ((ShopHeaderViewHolder) holder).shopBanner.setBannerAnimation(Transformer.ZoomOut);
            //设置自动轮播，默认为true
            ((ShopHeaderViewHolder) holder).shopBanner.isAutoPlay(true);
            //设置轮播时间
            ((ShopHeaderViewHolder) holder).shopBanner.setDelayTime(3000);
            //设置指示器位置（当banner模式中有指示器时）
            ((ShopHeaderViewHolder) holder).shopBanner.setIndicatorGravity(BannerConfig.CENTER);
            //下标是从1开始，同时解决下标越界问题
            ((ShopHeaderViewHolder) holder).shopBanner.setOnBannerListener(p -> {

            });
            ArrayList<String> imgs = new ArrayList<>();
            bannerLinks = new ArrayList<>();
            for (BannerBean bannerBean : bannerList) {
                imgs.add(RetrofitSource.image_url + bannerBean.getSlide_pic());
                bannerLinks.add(bannerBean.getSlide_url());
            }
            ((ShopHeaderViewHolder) holder).shopBanner.setImages(imgs);
            ((ShopHeaderViewHolder) holder).shopBanner.start();
        }
    }

    @Override
    public int getItemCount() {
        return goodList.size() + 1;
    }

    public void setBanner(List<BannerBean> data) {
        this.bannerList = data;
    }

    class ShopHeaderViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.shop_banner)
        Banner shopBanner;

        ShopHeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class GoodItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_good_pic)
        ImageView ivGoodPic;
        @BindView(R.id.iv_self_support)
        ImageView ivSelfSupport;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_vip_price)
        TextView tvVipPrice;
        @BindView(R.id.tv_normal_price)
        TextView tvNormalPrice;
        @BindView(R.id.iv_becom_vip)
        ImageView ivBecomVip;
        @BindView(R.id.item_shop)
        LinearLayout itemShop;

        GoodItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
